public class Board
{
    private Player[] players;
    private Cards cardDeck;
    private BoardSquare[] board;
   
    public Board()
    {
        int numberOfPlayers = 4;
        int numberOfSquares = 60;
       
        // set up players
        players = new Player[numberOfPlayers];
        for (int i = 0; i < numberOfPlayers; i++)
        {
            // construct players and assign colors (0-3)
            // red=0, blue=1, yellow=2, green=3
            players[i] = new Player(i);
        }
       
        // set up card deck
        cardDeck = new Cards();
       
        // set up board squares
        board = new BoardSquare[numberOfSquares];
        int i = 0;
        // red row
        while (i < 15)
        {
            board[i] = new BoardSquare(false, 0, 0);
            if (i == 1)
                board[i].setSlide(3);
            else if (i == 9)
                board[i].setSlide(4);
            else if (i == 2)
                board[i].setIsSZEntrance(true);
            i++;
        }
         // blue row
        while (i < 30)
        {
            board[i] = new BoardSquare(false, 1, 0);
            if (i == 16)
                board[i].setSlide(3);
            else if (i == 24)
                board[i].setSlide(4);
            else if (i == 17)
                board[i].setIsSZEntrance(true);
            i++;
        }
         // yellow row
        while (i < 45)
        {
            board[i] = new BoardSquare(false, 2, 0);
            if (i == 31)
                board[i].setSlide(3);
            else if (i == 39)
                board[i].setSlide(4);
            else if (i == 32)
                board[i].setIsSZEntrance(true);
            i++;
        }
         // green row
        while (i < 60)
        {
            board[i] = new BoardSquare(false, 3, 0);
            if (i == 46)
                board[i].setSlide(3);
            else if (i == 54)
                board[i].setSlide(4);
            else if (i == 47)
                board[i].setIsSZEntrance(true);
            i++;
        }
    }
   
    public int drawCard()
    {
        return cardDeck.drawTopCard();
    }
   
    public void slide(int player, int pawn)
    {
        int square = players[player].getPawnPosition(pawn);
        int slideDistance = board[square].getStartSlide();
        int slideColor = board[square].getColorPanel();
        if (slideColor != player)
        {
            for (int i = 0; i < slideDistance; i++)
                move(player, pawn, 1);
        }
    }
   
    public void bump(int square)
    {
        int color = board[square].getPawnColor(); // get color of pawn to be bumped
        int p = players[color].getPawnAtPosition(square); // tell player to find their pawn
        players[color].movePawnToStart(p); // movie it to start
    }
       
    public boolean isBump(int square)
    {
        // is there a pawn on the board square?
        boolean bump = false;
        int pawnIndex;
        int color = board[square].getPawnColor();
       
        if (color >= 0)
        {
            // check that pawn is not in SZ
            pawnIndex = players[color].getPawnAtPosition(square);
            bump = !players[color].getSafetyZone(pawnIndex);
        }
       
        return bump;
    }
   
    public int checkWinner()
    {
        int winner = -1;
        for (int i = 0; i < 4; i++)
        {
            if (players[i].pawnsInHome() == 4)
            {
                winner = i;
            }
        }
        return winner;
    }

    /*
    public boolean moveExists(int player, int card)
    {
        return players[player].moveExists(card);
    }
     *
     */
    public void move(int player, int pawn, int movement)
    {
    	boolean safe = players[player].getSafetyZone(pawn);
        int square = (players[player].getPawnPosition(pawn) + movement)%60;
        
        // reset square pawn is leaving
        if (!players[player].getStart(pawn))
        {
            if (safe)
            	board[players[player].getPawnPosition(pawn)].setSafeColor(-1);
            else
            	board[players[player].getPawnPosition(pawn)].setColorPawn(-1);
        }
        
        // bump
        if (isBump(square))
        {
            bump(square);
        }
        
        // move
        players[player].move(pawn, movement);
        safe = players[player].getSafetyZone(pawn);
        
        // set square pawn moves to
        if (safe)
        	board[players[player].getPawnPosition(pawn)].setSafeColor(player);
        else
        {
        	board[players[player].getPawnPosition(pawn)].setColorPawn(player);
        }
        
    }

    public boolean getStart(int player, int pawn)
    {
        return players[player].getStart(pawn);
    }

    public String toString()
    {
        String display = "";
        for (int i = 0; i < 60; i++)
        {
            display = display + (board[i].getColorPanel() + 1) + " ";
        }
        display = display + " Color Panel\n";
        for (int i = 0; i < 60; i++)
        {
            display = display + board[i].getStartSlide() + " ";
        }
        display = display + " Slides\n";
        for (int i = 0; i < 60; i++)
        {
            display = display + (board[i].getPawnColor() + 1) + " ";
        }
        display = display + " Pawns\n";
        for (int i = 0; i < 60; i++)
        {
            display = display + (board[i].getSafeColor() + 1) + " ";
        }
        display = display + " Safe Pawns\n";
        for (int i = 0; i < 4; i++)
        {
        	for (int j = 0; j < 4; j++)
        	{
        		if (players[i].getSafetyZone(j))
        		{
        			display = display + "Player " + (i+1) + "'s pawn " + (j+1) + " is safe.\n";
        		}
        	}
        }
        return display;
    }

}


